package ch.fusun.baron.basic.client.ui.accountproperty;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import ch.fusun.baron.basic.client.ui.Activator;
import ch.fusun.baron.basic.command.SetCoatOfArmsCommand;
import ch.fusun.baron.clientapp.login.AccountProperty;
import ch.fusun.baron.coatofarms.ui.COAColor;
import ch.fusun.baron.coatofarms.ui.CoatOfArmsData;
import ch.fusun.baron.coatofarms.ui.background.Backgrounds;
import ch.fusun.baron.coatofarms.ui.background.COABackground;
import ch.fusun.baron.core.command.GameCommand;
import ch.fusun.baron.core.rmi.User;
import ch.fusun.baron.player.Dynasty;

/**
 * Select the coat of arms for the {@link Dynasty}
 */
public class CoatOfArmsAccountProperty implements AccountProperty {

	private transient ComboViewer layoutCombo;
	private transient ComboViewer color1Combo;
	private transient ComboViewer color2Combo;

	private CoatOfArmsData data;

	/**
	 * Empty constructor
	 */
	public CoatOfArmsAccountProperty() {
	}

	@Override
	public void createUi(Composite parentArea) {

		new Label(parentArea, SWT.NONE).setText("Background:");
		layoutCombo = new ComboViewer(parentArea, SWT.READ_ONLY);
		layoutCombo.setContentProvider(new ArrayContentProvider());
		layoutCombo.setInput(Backgrounds.values());
		layoutCombo.setSelection(new StructuredSelection(
				Backgrounds.VerticallyDivided));

		new Label(parentArea, SWT.NONE).setText("Color 1:");
		color1Combo = new ComboViewer(parentArea, SWT.READ_ONLY);
		color1Combo.setContentProvider(new ArrayContentProvider());
		color1Combo.setInput(COAColor.values());
		color1Combo.setSelection(new StructuredSelection(COAColor.AZURE));

		new Label(parentArea, SWT.NONE).setText("Color 2:");
		color2Combo = new ComboViewer(parentArea, SWT.READ_ONLY);
		color2Combo.setContentProvider(new ArrayContentProvider());
		color2Combo.setInput(COAColor.values());
		color2Combo.setSelection(new StructuredSelection(COAColor.OR));

		new Label(parentArea, SWT.NONE).setText("Preview:");
		final Label coa = new Label(parentArea, SWT.NONE);
		coa.setImage(Activator.getImageForCOA(getData()));

		ISelectionChangedListener listener = new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				data = getData();
				coa.setImage(Activator.getImageForCOA(data));
				coa.redraw();
			}
		};

		layoutCombo.addSelectionChangedListener(listener);
		color1Combo.addSelectionChangedListener(listener);
		color2Combo.addSelectionChangedListener(listener);
		data = getData();
	}

	@Override
	public GameCommand getCommand(User user) {
		return new SetCoatOfArmsCommand(data, user);
	}

	private CoatOfArmsData getData() {
		COABackground background = ((Backgrounds) ((StructuredSelection) layoutCombo
				.getSelection()).getFirstElement()).createLayout(
				(COAColor) ((StructuredSelection) color1Combo.getSelection())
						.getFirstElement(),
				(COAColor) ((StructuredSelection) color2Combo.getSelection())
						.getFirstElement());
		CoatOfArmsData data = new CoatOfArmsData(background, null);
		return data;
	}

}
